<template>
  <div id="workCheckIndex">
    <!--上部搜索条件组件-->
    <topSearch :form-inline="formInline" @toSearch="toSearch"></topSearch>
    <div class="table-conBtn">
      <el-button type="warning" icon="el-icon-check" @click="handleAllPass">
        公开展示
      </el-button>
      <el-button
        type="warning"
        icon="el-icon-check"
        @click="handlePersonalPass"
      >
        仅主页展示
      </el-button>
      <el-button type="warning" icon="el-icon-close" plain @click="notPass()">
        不通过
      </el-button>
      <freshCom @searchList="searchList"></freshCom>
      <selectColumn
        ref="selectCum"
        @setColumn="setColumns"
        :nameList="columnList"
      ></selectColumn>
    </div>
    <!--下部table-->
    <el-table
      v-loading="loading"
      :data="tableData"
      stripe
      style="width: 100%"
      @selection-change="handleSelectionChange"
      max-height="490"
      ref="table"
    >
      <el-table-column
        type="selection"
        width="50"
        align="center"
        :selectable="selectAble"
      ></el-table-column>
      <el-table-column type="index" width="50" label="序号">
        <template slot-scope="scope">
          {{
            scope.$index
              | filterPage(searchParams.pageNum, searchParams.pageSize)
          }}
        </template>
      </el-table-column>
      <el-table-column
        v-if="columnSHow.avatar"
        label="头像"
        min-width="100"
        align="center"
      >
        <template slot-scope="scope">
          <listImgString :imgInfo="scope.row.avatar"></listImgString>
        </template>
      </el-table-column>
      <el-table-column
        v-if="columnSHow.nickname"
        label="昵称"
        prop="nickname"
        min-width="80"
      >
        <template slot-scope="scope">
          {{ scope.row.nickname }}
        </template>
      </el-table-column>
      <el-table-column
        v-if="columnSHow.dpAccount"
        label="大鹏号"
        prop="dpAccount"
        min-width="80"
      >
        <template slot-scope="scope">
          {{ scope.row.dpAccount }}
        </template>
      </el-table-column>
      <el-table-column
        v-if="columnSHow.workType"
        label="类型"
        prop="workType"
        min-width="70"
      >
        <template slot-scope="scope">
          <span
            v-if="scope.row.workType"
            class="tag-span"
            :class="scope.row.workType | filterClass('workType')"
            >{{ scope.row.workType | filterName('workType') }}</span
          >
        </template>
      </el-table-column>
      <el-table-column
        v-if="columnSHow.linkActivityName"
        label="关联活动"
        min-width="80"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.linkActivityName">{{
            scope.row.linkActivityName
          }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="columnSHow.content"
        label="文字内容"
        prop="content"
        class-name="text-overflow"
        min-width="100"
      >
        <template slot-scope="scope">
          <span
            v-if="scope.row.content"
            class="textContent"
            v-html="filterContent(scope.row)"
            @click="showDetail(scope.row)"
            >{{ scope.row.content }}</span
          >
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="columnSHow.imgInfo"
        label="视图内容"
        min-width="100"
        align="center"
      >
        <template slot-scope="scope">
          <!--图片-->
          <span v-if="!scope.row.video">
            <listImgShow :imgInfo="scope.row.imgInfo"></listImgShow>
          </span>
          <!--视频-->
          <span v-else class="img-box" @click="videoPlay(scope.row.video)">
            <div class="img-inner-box">
              <el-image fit="contain" :src="scope.row.imgInfo"></el-image>
            </div>
          </span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="columnSHow.submit"
        label="创建时间"
        prop="submit"
        min-width="120"
      >
        <template slot-scope="scope">
          {{ scope.row.submit | timeToTime }}
        </template>
      </el-table-column>
      <el-table-column
        v-if="columnSHow.userProperty"
        label="用户属性"
        prop="userProperty"
        min-width="80"
      >
        <template slot-scope="scope">
          <span
            v-if="scope.row.userProperty"
            class="tag-span"
            :class="scope.row.userProperty | filterClass('checkUserAttr')"
            >{{ scope.row.userProperty | filterName('checkUserAttr') }}</span
          >
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="columnSHow.mSubmit"
        label="机审时间"
        prop="submit"
        min-width="120"
      >
        <template slot-scope="scope">
          {{ scope.row.submit | timeToTime }}
        </template>
      </el-table-column>
      <el-table-column
        v-if="columnSHow.machineStatu"
        label="机审结果"
        prop="machineStatu"
        min-width="90"
      >
        <template slot-scope="scope">
          <span
            v-if="scope.row.machineStatu === 'PASS'"
            class="tag-span primary"
            >通过</span
          >
          <span
            v-if="scope.row.machineStatu === 'UNPASS'"
            class="tag-span danger hand"
            >未通过</span
          >
        </template>
      </el-table-column>
      <el-table-column
        v-if="columnSHow.checkStatu"
        label="人工审核结果"
        prop="checkStatu"
        min-width="110"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.checkStatu === 'PASS'" class="tag-span primary"
            >通过</span
          >
          <span
            v-if="scope.row.checkStatu === 'UNCHECKED'"
            class="tag-span warning"
            >未审核</span
          >
          <span
            v-if="scope.row.checkStatu === 'UNPASS'"
            class="tag-span danger hand"
            >未通过</span
          >
        </template>
      </el-table-column>
      <el-table-column
        v-if="columnSHow.categoryId"
        label="学院"
        prop="categoryId"
        min-width="110"
      >
        <template slot-scope="scope">
          <span
            v-if="scope.row.categoryId"
            class="tag-span"
            :class="scope.row.categoryId | filterClass('collegeType')"
            >{{ scope.row.categoryId | filterName('collegeType') }}</span
          >
        </template>
      </el-table-column>
      <el-table-column
        v-if="columnSHow.displayRule"
        label="展示方式"
        prop="displayRule"
        min-width="100"
      >
        <template slot-scope="scope">
          <span
            v-if="scope.row.displayRule"
            class="tag-span"
            :class="scope.row.displayRule | filterClass('ruleType')"
            >{{ scope.row.displayRule | filterName('ruleType') }}</span
          >
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="columnSHow.checkName"
        label="审核人"
        prop="checkName"
        min-width="80"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.checkName"> {{ scope.row.checkName }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="columnSHow.checkTime"
        label="人工审核时间"
        prop="checkTime"
        min-width="110"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.checkTime">
            {{ scope.row.checkTime | timeToTime }}</span
          >
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="columnSHow.labels"
        label="显性标签"
        prop="labelPrope"
        min-width="260"
      >
        <template slot-scope="scope">
          <explicitTag
            :labels="scope.row.labels"
            :showContentId="scope.row.id"
            @refresh="refresh"
          ></explicitTag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="200">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.checkStatu === 'UNCHECKED'"
            type="text"
            @click="singlePublicPass('ALL', scope.row.id)"
          >
            公开展示
          </el-button>
          <el-button
            v-if="scope.row.checkStatu === 'UNCHECKED'"
            type="text"
            @click="singlePersonalPass('PERSONAL', scope.row.id)"
          >
            仅主页可见
          </el-button>
          <el-button
            v-permission="'wh:system:worksContentExamined:noadopt'"
            v-if="scope.row.checkStatu === 'UNCHECKED'"
            type="text"
            @click="handlNotPassWord(scope.row.id)"
          >
            不通过
          </el-button>
          <el-button
            v-permission="'wh:system:worksContentExamined:detail'"
            v-if="scope.row.checkStatu !== 'UNPASS'"
            type="text"
            @click="showDetail(scope.row)"
          >
            查看详情
          </el-button>
          <el-button
            v-if="scope.row.checkStatu === 'UNPASS'"
            type="text"
            @click="showFailReason(scope.row)"
          >
            未通过理由
          </el-button>
          <el-button
            v-if="scope.row.checkStatu === 'PASS'"
            type="text"
            @click="deleteWork(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-全局组件-->
    <pageCom
      :searchParams="searchParams"
      :total="total"
      @searchList="searchList"
    ></pageCom>
    <dialogDetailCom
      v-if="dialogData.isShow"
      :dialog-data="dialogData"
    ></dialogDetailCom>
    <!--播放视频组件-->
    <showVideo v-if="videoData.isShow" :video-data="videoData"></showVideo>
    <!-- 不通过理由弹窗 -->
    <del-dialog
      :del-dialog-data="delDialogData"
      @reasonDel="failRequest"
    ></del-dialog>
  </div>
</template>

<script>
import topSearch from '@component/contentCheck/workCheck/topSearch' // 上部搜索条件
import dialogDetailCom from '@common/dialogDetailCom' // 详情-弹窗-公共组件
import showVideo from '@common/showVideo'
import delDialog from '@common/delDialog' // 不通过理由弹窗
import { filterCon, getVideoPara } from '@utils/pubFunction' // 内容筛选
import { getVideoImg } from '@utils/getFaceImg'
import { saveScrollMixins } from '@/mixins/saveScroll'
import { columnMixin } from '@/mixins/columnShowHide'
import explicitTag from '@common/explicitTag'
import listImgString from '@common/listImgString' // 视图预览(返回值为字符串结构)-公共组件
import {
  getSearchList,
  publicSingleAPI,
  publicMultAPI,
  personalSingleAPI,
  personalMultAPI,
  failSingleRequestAPI,
  failMultRequestAPI,
  deleteRequestAPI
} from '@/api/contentCheck/workContent'
export default {
  name: 'workCheck',
  msg: '作品内容审核',
  mixins: [saveScrollMixins, columnMixin],
  components: {
    topSearch,
    dialogDetailCom,
    showVideo,
    delDialog,
    explicitTag,
    listImgString
  },
  data() {
    return {
      loading: true,
      total: 0,
      alldisabled: true, // 公开展示按钮禁用状态
      mdisabled: true, // 仅主页展示按钮禁用状态
      notdisabled: true, // 不通过按钮禁用状态
      tableData: [], // table内容
      tableTemp: [], // table替代
      tempSize: 0,
      searchParams: {
        pageNum: 1, // 当前页数
        pageSize: 10 // 页面显示条数
      },
      formInline: {
        // 搜索条件
        categoryId: '', // 选择学院
        checkStatus: '', // 人审结果
        maintainCheckStatus: '', // 机审结果
        content: '', // 内容查询
        sortRule: '', // 排序规则
        workType: '', // 作品类型
        timeRule: '', // 时间
        timeLine: '', // 时间线
        checkUserId: '', // 审核人
        displayRule: '', // 展示方式
        startTime: '', // 开始时间
        endTime: '', // 结束时间
        userValue: '',
        userType: '',
        activityId: '',
        userAttribute: '', // 用户属性
        createTime: []
      },
      displayRule: '', // 展示方式 ALL(公开展示) PERSONAL(仅主页可见)
      dialogData: {
        isShow: false, // 默认隐藏
        type: '', // 类型，workDetail-作品详情，jobDetail-作业详情
        id: '', // 编辑时，传递的参数
        title: ''
      },
      dailgExplicTag: {},
      videoData: {
        // 视频播放弹窗
        isShow: false,
        vid: ''
      },
      polivNum: {
        polyvUserId: '',
        polyvSecretKey: ''
      },
      multipleSelection: [],
      powerStr: 'system:worksContentExamined:list', // 是否查询的标识
      powerAry: [], // 可显示的权限
      canShowPage: false, // 是否显示当前页面
      delDialogData: {
        isShow: false,
        params: '',
        type: '不通过'
      },
      id: '',
      isDelete: false,
      columnList: [
        { label: '头像', value: 'avatar' },
        { label: '昵称', value: 'nickname' },
        { label: '大鹏号', value: 'dpAccount' },
        { label: '类型', value: 'workType' },
        { label: '关联活动', value: 'linkActivityName' },
        { label: '文字内容', value: 'content' },
        { label: '视图内容', value: 'imgInfo' },
        { label: '创建时间', value: 'submit' },
        { label: '用户属性', value: 'userProperty' },
        { label: '机审时间', value: 'mSubmit' },
        { label: '机审结果', value: 'machineStatu' },
        { label: '人工审核结果', value: 'checkStatu' },
        { label: '学院', value: 'categoryId' },
        { label: '展示方式', value: 'displayRule' },
        { label: '审核人', value: 'checkName' },
        { label: '人工审核时间', value: 'checkTime' },
        { label: '显性标签', value: 'labels' }
      ]
    }
  },
  activated() {
    this.toTableScrollHeight()
  },
  mounted() {
    // 获取权限
    // this.getPower()
    this.$nextTick(() => {
      setTimeout(() => {
        this.setTableScrollHeight()
        this.$refs['selectCum'].setChoose(this.columnAry)
      }, 1000)
    })
  },
  methods: {
    /* 获取权限 */
    getPower() {
      let id = this.$route.query.mId
      this.getCurPageMenu(id).then((res) => {
        this.powerAry = res
        this.canShowPage = true
        if (!res.includes(this.powerStr)) {
          this.canGetList = false
          this.alertPower(this.powerStr)
        } else {
        }
      })
    },
    /* 获取系统配置-保利威视 */
    getPolivNum() {
      let _this = this
      getVideoPara().then(
        (res) => {
          // 接口请求正常
          this.polivNum.polyvUserId = res.polyvUserId
          this.polivNum.polyvSecretKey = res.polyvSecretKey
          let n = 0
          if (!this.tableTemp || this.tableTemp.length === 0) {
            return
          }
          this.tableTemp.forEach((item, index) => {
            _this.getImg(item.video, index).then(
              (data) => {
                n++
                _this.setData(n)
              },
              () => {
                n++
                _this.setData(n)
              }
            )
          })
        },
        (res) => {
          // 接口请求不正常正常
          _this.tableData = [..._this.tableTemp]
        }
      )
    },
    /*  符合条件后，给table赋值 */
    setData(n) {
      if (n === this.tempSize) {
        this.loading = false
        this.tableData = [...this.tableTemp]
      }
    },
    /* 获取封面图 */
    getImg(video, index) {
      if (!video) return Promise.resolve(false)
      let _this = this
      return getVideoImg(video, this.polivNum).then(
        (res) => {
          _this.tableTemp[index].imgInfo = res
          return Promise.resolve(true)
        },
        (error) => {
          return Promise.reject(error)
        }
      )
    },
    /* 播放视频 */
    videoPlay(vid) {
      if (!vid) {
        this.$message.warning('无视频资源！')
        return
      }
      this.videoData.vid = vid
      this.videoData.isShow = true
    },
    /* 获取列表 */
    searchList() {
      this.loading = true
      let params = Object.assign({}, this.searchParams, this.formInline)
      delete params.createTime
      getSearchList(
        params,
        (response) => {
          this.loading = false
          this.tableTemp = response.rows
          this.tableData = response.rows

          this.tempSize = response.rows.length
          this.total = response.total
          this.getPolivNum()
        },
        (response) => {
          this.loading = false
          this.$message({
            message: response.message,
            type: 'warning'
          })
        }
      )
    },
    // table选择
    handleSelectionChange(val) {
      this.multipleSelection = val
      this.alldisabled = false
      this.mdisabled = false
      this.notdisabled = false
      if (this.multipleSelection.length === 0) {
        this.notdisabled = true
        this.alldisabled = true
        this.mdisabled = true
      }
    },
    /* 过滤返回评论内容 */
    filterContent(item) {
      return filterCon(item)
    },
    // 公开展示
    handleAllPass(id) {
      if (this.multipleSelection.length <= 0) {
        this.$message.warning('请至少选择一条数据！')
        return
      }
      this.$confirm(
        `确认要审核通过选中的${this.multipleSelection.length}条数据吗?`,
        '系统提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        this.getIds().then((ids) => {
          let params = {
            ids,
            displayRule: 'ALL'
          }
          publicMultAPI(params, (response) => {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.searchList()
          })
        })
      })
    },
    /* 获取选中数据的id集合 */
    getIds() {
      let _this = this
      let ids = []
      let muLength = this.multipleSelection.length - 1
      return new Promise((resolve, reject) => {
        _this.multipleSelection.forEach((item, index) => {
          ids.push(item.id ? item.id : null)
          if (index === muLength) {
            resolve(ids)
          }
        })
      })
    },
    singlePublicPass(type, id) {
      this.displayRule = type
      this.$confirm('确定审核通过该条作品内容审核信息吗？', '通过提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.singlePublicRequest(id)
        })
        .catch(() => {})
    },
    singlePublicRequest(id) {
      let params = {
        displayRule: 'ALL'
      }
      publicSingleAPI(id, params, (response) => {
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.searchList()
      })
    },
    // 仅主页展示通过
    handlePersonalPass(id) {
      if (this.multipleSelection.length <= 0) {
        this.$message.warning('请至少选择一条数据！')
        return
      }
      this.$confirm(
        `确认要审核通过选中的${this.multipleSelection.length}条数据吗?`,
        '系统提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        this.getIds().then((ids) => {
          let params = {
            ids,
            displayRule: 'PERSONAL'
          }
          personalMultAPI(params, (response) => {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.searchList()
          })
        })
      })
    },
    singlePersonalPass(type, id) {
      this.displayRule = type
      this.$confirm('确定审核通过该条作品内容审核信息吗？', '通过提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.singlePersonalRequest(id)
      })
    },
    singlePersonalRequest(id) {
      let params = {
        displayRule: this.displayRule
      }
      personalSingleAPI(id, params, (response) => {
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.searchList()
      })
    },
    /* 批量不通过 */
    notPass() {
      if (this.multipleSelection.length <= 0) {
        this.$message.warning('请至少选择一条数据！')
        return
      }
      let ids = []
      this.multipleSelection.forEach((item) => {
        ids.push(item.id)
      })
      this.id = ids
      this.delDialogData.isShow = true
    },
    /* 不通过单个 */
    handlNotPassWord(id) {
      this.id = id
      this.delDialogData.isShow = true
    },
    failRequest(reason) {
      if (this.isDelete) {
        this.deleteRequest(this.id, reason)
        return false
      }
      if (this.id instanceof Array) {
        this.failMultRequest(this.id, reason)
      } else {
        this.failSingleRequest(this.id, reason)
      }
      this.delDialogData.isShow = false
    },
    // 不通过单条请求
    failSingleRequest(id, reason) {
      let type = this.delDialogData.type
      this.$confirm(
        `${type}理由：${reason} 确认${type}该条信息？`,
        '系统提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        let params = {
          reason
        }
        failSingleRequestAPI(id, params, (response) => {
          this.$message({
            message: '成功',
            type: 'success'
          })
          this.searchList()
        })
      })
    },
    // 不通过多条请求
    failMultRequest(ids, reason) {
      let type = this.delDialogData.type
      this.$confirm(
        `${type}理由：${reason} 确认${type}该条信息？`,
        '系统提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        let params = { ids, reason }
        failMultRequestAPI(params, (response) => {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.searchList()
        })
      })
    },
    // 删除
    deleteWork(id) {
      this.id = id
      this.isDelete = true
      this.delDialogData.isShow = true
    },
    // 删除单条请求
    deleteRequest(id, reason) {
      let type = '删除'
      this.$confirm(
        `${type}理由：${reason} 确认${type}该条信息？`,
        '系统提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        let params = {
          reason,
          isJson: true
        }
        deleteRequestAPI(id, params, (response) => {
          this.searchList()
        })
        this.isDelete = false
      })
    },
    /* 换页 */
    changePage(page) {
      this.searchParams.pageNum = page
      this.searchList()
    },
    /* 刷新列表 */
    refresh() {
      this.toSearch()
    },
    /* 搜索 */
    toSearch() {
      this.searchParams.pageNum = 1
      this.searchList()
    },
    /* 显示详情 */
    showDetail(item) {
      if (item.checkStatu === 'UNPASS') {
        return
      }
      this.dialogData.type = 'workDetail'
      this.dialogData.id = item.id
      this.dialogData.isShow = true
      this.dialogData.title = '作品内容审核详细'
    },
    // 显示未通过理由
    showFailReason(row) {
      this.$alert(row.reason, '不通过理由', {
        showConfirmButton: false
      }).catch(() => {})
    },
    selectAble(row, index) {
      if (row.checkStatu === 'UNCHECKED') {
        return true
      } else {
        return false
      }
    }
  }
}
</script>

<style lang="less">
#workCheckIndex {
  @import url('~@common/style/imgBox.less');
  .textContent {
    cursor: pointer;
  }
  .text-overflow {
    .cell {
      max-height: 140px;
    }
  }
}
</style>
